package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import com.entity.Info;
import com.param.Init;
import com.param.MyTools;

public class SaveImgAction {
	private File[] img;
	private String[] imgContentType;
	private String[] imgFileName;
	
	
	public String save(){
		if(img!=null){
			for(int i=0;i<img.length;i++){
				//设置统一文件名
				String id =MyTools.getId(20);
				String name=MyTools.format4.format(new Date())+File.separator+id;
				//获得文件后缀
				File file=new File(Init.imgPath,name);
				
				if(!file.getParentFile().exists()){
					file.getParentFile().mkdirs();
				}
				
				InputStream in=null;
				OutputStream os=null;
				try{
					in=new FileInputStream(img[i]);
					os=new FileOutputStream(file);
					//加密
					Init.encryption(os);
					int len=0;
					byte[] bs=new byte[1024];
					while((len=in.read(bs))!=-1){
						os.write(bs, 0, len);
					}
					
				}
				catch(Exception e){
					e.printStackTrace();
				}
				finally {
					try {
						if(os!=null){
							os.close();
						}
						if(in!=null){
							in.close();
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				Info info=new Info(id, imgFileName[i], name);
				Init.imgMap.put(id, info);
			}
			
		}
		System.out.println(Init.imgMap);
		return "success";
	}


	public void setImg(File[] img) {
		this.img = img;
	}


	public void setImgContentType(String[] imgContentType) {
		this.imgContentType = imgContentType;
	}


	public void setImgFileName(String[] imgFileName) {
		this.imgFileName = imgFileName;
	}
	
	
	
	
}
